package com.hotel.controller;

import com.hotel.pojo.Food;
import com.hotel.pojo.FoodType;
import com.hotel.utils.WebUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by LinYaTing on 2018/11/25 22:24
 */
@WebServlet(name = "FoodServlet", urlPatterns = "/food")
public class FoodServlet extends BaseServlet {

    private Object uri;

    //食物列表展示
    public Object listFood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //调用业务逻辑
            List<Map<String, Object>> foodList = foodService.findAll();
            //保存
            request.setAttribute("foodList", foodList);
            //跳转
            uri = request.getRequestDispatcher("/sys/food/food_list.jsp");
        return uri;
    }

    //进入添加页面
    public Object viewSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //调用业务逻辑
            List<FoodType> typeList = foodTypeService.findNameAndId();
            //保存
            request.setAttribute("typeList", typeList);
            //跳转
            uri = request.getRequestDispatcher("/sys/food/food_save.jsp");
            return uri;
    }

    //添加食物
    public Object addFood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            //获得数据封装数据
            Food food = new Food();
            BeanUtils.populate(food, request.getParameterMap());
            //调用业务逻辑
            foodService.save(food);
            //跳转
            uri = request.getRequestDispatcher("/food?method=listFood");
            return uri;
    }

    //搜索
    public Object search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获得数据封装数据
            String keyWord = request.getParameter("keyWord");
            //调用业务逻辑
            List<Map<String, Object>>foodList = foodService.findFoodByKeyWord(keyWord);
            //保存
            request.setAttribute("foodList", foodList);
            //跳转
            uri = request.getRequestDispatcher("/sys/food/food_list.jsp");
            return uri;
    }

    //进入更新页面
    public Object viewUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获得数据封装数据
            String id = request.getParameter("id");
            //调用业务逻辑
            Map<String, Object> food = foodService.findFoodById(Integer.parseInt(id));
            List<FoodType> typeList = foodTypeService.findNameAndId();
            //保存
            request.setAttribute("food", food);
            request.setAttribute("typeList", typeList);
            //跳转
            uri = request.getRequestDispatcher("/sys/food/food_update.jsp");
            return uri;
    }

    //更新
    public Object update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

            //获得数据封装数据
            Food food = new Food();
            BeanUtils.populate(food, request.getParameterMap());
            //调用业务逻辑
            foodService.update(food);
            //跳转
            uri = request.getRequestDispatcher("/food?method=listFood");

        return uri;
    }

    //删除
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获得数据封装数据
            String id = request.getParameter("id");
            //调用业务逻辑
            foodService.delete(Integer.parseInt(id));
            //跳转
            uri = "/food?method=listFood";
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/food?method=listFood";
        }
        return uri;
    }

}
